//
//  Player_waveform.m
//  SoundPill
//
//  Created by 宋旭东 on 15/4/22.
//  Copyright (c) 2015年 宋旭东. All rights reserved.
//

#import "Player_waveform.h"

@interface Player_waveform ()
{
    float offset;
    NSInteger index;
}
@end

@implementation Player_waveform
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.waveArray = [[NSArray alloc] init];
        offset = [UIScreen mainScreen].bounds.size.width / 2;
        index = 0;
    }
    return self;
}
- (void)getWaveform:(NSArray *)waveform
{
    
}
- (void)getAudioPresses:(float)presses offset:(float) newOffset
{
    index = _waveArray.count * presses;
    offset = newOffset;
    if (index < _waveArray.count) {
          [self setNeedsDisplay];
    }
}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    float oldoffset = [UIScreen mainScreen].bounds.size.width / 2;
    // Drawing code
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineCap(context, kCGLineCapSquare); //线条样式
    CGContextSetLineWidth(context, 3); //线条宽度

    CGContextSetRGBStrokeColor(context, 0.996f, 1.000f, 1.000f, 1.000f);
    CGContextBeginPath(context);
    for (int i = index; i < _waveArray.count; i++) {
            float newoffset = (6*i) + oldoffset;
            NSString *H = _waveArray[i];
        if (index == i) {
            CGContextMoveToPoint(context, newoffset, self.frame.size.height / 2 - 2);
            CGContextAddLineToPoint(context, newoffset, self.frame.size.height / 2 - H.floatValue * 0.1);
        } else if (i == index + 1) {
            CGContextMoveToPoint(context, newoffset, self.frame.size.height / 2 - 2);
            CGContextAddLineToPoint(context, newoffset, self.frame.size.height / 2 - H.floatValue * 0.2);
        } else if (i == index + 2) {
            CGContextMoveToPoint(context, newoffset, self.frame.size.height / 2 - 2);
            CGContextAddLineToPoint(context, newoffset, self.frame.size.height / 2 - H.floatValue * 0.2);
        } else if (i == index + 3) {
            CGContextMoveToPoint(context, newoffset, self.frame.size.height / 2 - 2);
            CGContextAddLineToPoint(context, newoffset, self.frame.size.height / 2 - H.floatValue * 0.3);
        } else if (i == index + 4) {
            CGContextMoveToPoint(context, newoffset, self.frame.size.height / 2 - 2);
            CGContextAddLineToPoint(context, newoffset, self.frame.size.height / 2 - H.floatValue * 0.4);
        } else if (i == index + 5) {
            CGContextMoveToPoint(context, newoffset, self.frame.size.height / 2 - 2);
            CGContextAddLineToPoint(context, newoffset, self.frame.size.height / 2 - H.floatValue * 0.5);
        } else if (i == index + 6) {
            CGContextMoveToPoint(context, newoffset, self.frame.size.height / 2 - 2);
            CGContextAddLineToPoint(context, newoffset, self.frame.size.height / 2 - H.floatValue * 0.6);
        }
        else {
            CGContextMoveToPoint(context, newoffset, self.frame.size.height / 2 - 2);
            CGContextAddLineToPoint(context, newoffset, self.frame.size.height / 2 - H.floatValue * 0.7);
        }
        
        
        
//        CGContextAddLineToPoint(context, newoffset, self.frame.size.height / 2 - H.floatValue * 0.7);
    }
    CGContextStrokePath(context);
}


@end
